<?php
/**
* File that contains Application_Form_Admin_editBusline class.
* @package busmap
*/

/**
* Application_Form_Admin_editBusline class generates a form that is being used when editing buslines.
* Form consists a text input for editing the busline name.
* There are two disabled text inputs where a inserted date and number of busstops are being shown.
* Select field is used to show and edit the color assigned to the busline.
* There is a textarea for the description.
* A subform called busstopsForm is being generated to show the busstops that the busline goes through.
* @package busmap
*/
class Application_Form_Admin_editBusline extends Zend_Form
{
	/**
	* Initialize generates the form and all fields described in the class description.
	* Also sets some error messages and CSS parameters.
	* For each color, an hidden field with the background color set to the corresponding color is being generated. These hidden fields are later used to assign background colors to color select options (because Zend can't assign CSS to <option> tag directly (!) )
	*/
    public function init()
    {
        $this->setMethod('post');
		$this->setOptions(array('id'=>'editBuslinesForm'));

        $this->addElement(
            'hidden', 'id', array(
                'required' => true
            ));
 
        $this->addElement(
            'text', 'name', array(
                'required' => true,
                'filters'    => array('StringTrim'),
				'class' => 'bold-text'
            ));
		$this->name->addErrorMessage('Bussiliini nimi ei saa olla tühi');
		$this->name->addDecorator('HtmlTag', array('tag'=>'div', 'openOnly'=>true, 'class'=>'data-listing data-listing-cell'));
		$this->addElement(
            'text', 'addTime', array(
                'required' => false,
				'readonly' => true,
				'ignore' => true,
				'class' => 'medium-data readonly-data'
            ));
 		$this->addTime->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing'));

		
		$this->addElement(
            'select', 'colorId', array(
                'required' => true,
				'onChange' => 'colorMeChange(this)',
				'class' => 'colorSelecter'
            ));
		$colorMapper = new Application_Model_ColorMapper();
		$colors = $colorMapper->fetchAll();

		$this->colorId->addMultiOption(0, '-----');
		$this->addElement(
		'hidden', 'colorStyle-0', array(
			'required' => false,
			'ignore' => true,
			'id'=>'colorStyle-0',
			'style' => 'background:rgb(255,255,255);'
		));

		foreach($colors as $color){
			$this->colorId->addMultiOption($color->id, $color->name);
			$option = $this->colorId->getMultiOption($color->id);
			$this->addElement(
            'hidden', 'colorStyle-'.$color->id, array(
                'required' => false,
				'ignore' => true,
				'id'=>'colorStyle-'.$color->id,
				'style' => 'background-color:rgb('.$color->redValue.','.$color->greenValue.','.$color->blueValue.');'
            ));

		}

		$this->addElement(
            'text', 'nrStops', array(
                'required' => false,
				'readonly' => true,
				'ignore' => true,
				'class' => 'medium-data readonly-data'
            ));
 		$this->nrStops->addDecorator('HtmlTag', array('tag'=>'div', 'closeOnly'=>true, 'class'=>'data-listing'));

        $this->addElement(
            'textarea', 'description', array(
                'required' => true,
            ));
		$this->description->addErrorMessage('Bussiliini kirjeldus ei saa olla tühi');
		$this->description->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-big'));


		$busstopsForm = new Zend_Form_SubForm('busstopsForm');
		$busstopsForm->removeDecorator('fieldset');
		$busstopsForm->removeDecorator('DtDdWrapper');
		$busstopsForm->setName('busstopsForm');
		
		$this->addSubForm($busstopsForm, 'busstopsForm');
		$this->busstopsForm->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell busstop-listing'));
		
	    $this->addElement('submit', 'change', array(
            'ignore'   => true,
            'label'    => 'Muuda',
			'class'		=> 'submit-data'
            ));
		$this->change->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-button'));

	    $this->addElement('submit', 'delete', array(
            'ignore'   => true,
            'label'    => 'Kustuta',
			'class'		=> 'delete-data'
            ));
		$this->delete->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-button delete-data'));
 
		$this->clearElementLabels();
		
    }

	/**
	* This function is called when the delete request is received.
	* It generates two new buttons instead of the delete button. One of these buttons is the delete confirmation button. Pressing that sends the request back to the controller, but now with delete_confirm key.
	*/
	public function askDeleteConfirmation(){
		$this->removeElement('delete');

		$this->addElement('submit', 'back', array(
			 'ignore'   => true,
			 'label'	=>'Ära kustuta',
			 'class'	=>'submit-data'
		));
		$this->back->addDecorator('HtmlTag', array('tag'=>'div', 'openOnly'=>true, 'class'=>'data-listing data-listing-cell-button'));
		
		$this->addElement('submit', 'delete_confirm', array(
			 'ignore'   => true,
			 'label'	=>'Kustuta! [kinnitus]',
			 'class'	=>'delete-data'
		));
		$this->delete_confirm->addDecorator('HtmlTag', array('tag'=>'div', 'closeOnly'=>true, 'class'=>'data-listing data-listing-cell-button delete-data'));

		$this->clearElementLabels();

	}

	/**
	* This function colors the color select background with the corresponding busline color.
	* Method name is inspired by the Violent Femmes song Color Me Once, that appeared in the motion picture The Crow (1994).
	* http://www.youtube.com/watch?v=CLxSn0m-VTs
	*/
	public function colorMeOnce(){
		$selectedId = $this->colorId->getValue();
		if($selectedId){
			$style = $this->{'colorStyle-'.$selectedId}->getAttrib('style');
			$this->colorId->setAttrib('style', $style);
		}
	}

	/**
	* This function clears all default label and DtDd decorators from the form elements.
	* These decorators were unfeasible to stylize with CSS.
	*/
	public function clearElementLabels(){
		$elements = $this->getElements();
		foreach($elements as $element){
			$element->removeDecorator('label')->removeDecorator('DtDdWrapper');
		}
	}

	/**
	* This method populates the busstopForm subform with busstops send to it.
	* To the beginning of that form and between each busstop it adds an button that can be used to insert a new busstop to the corresponding place.
	*/
	public function populateWithBusstops($busstops, $buslineId){

		$this->busstopsForm->addElement('button', 'add0',
			array(
				'class'=>'add-new-busstop-between',
				'label'=>'LISA',
				'onClick'=>'showBusstopListing(-1, '.$buslineId.', this)'
			)
		);
		$this->busstopsForm->{'add0'}->removeDecorator('DtDdWrapper');

		if(count($busstops)){
			$busstopElements = array();
			foreach($busstops as $busstop_data){
				list($busstop, $uniqueId) = $busstop_data;
				$this->busstopsForm->addElement('checkbox', strval($uniqueId),
					array(
						'label'=>$busstop->name,
						'belongsTo'=>'stops',
						'checkedValue'=>$busstop->id,
						'checked'=>true
					)
				);
				$this->busstopsForm->$uniqueId->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'buslines-admin-busstop-checkbox'));
				$this->busstopsForm->$uniqueId->addDecorator('Label', array('class'=>'buslines-admin-busstop-name', 'placement'=>'append'));

				$this->busstopsForm->addElement('button', 'add'.$uniqueId,
					array(
						'class'=>'add-new-busstop-between',
						'label'=>'LISA',
						'onClick'=>'showBusstopListing('.$uniqueId.', '.$buslineId.', this)'
					)
				);
				$this->busstopsForm->{'add'.$uniqueId}->removeDecorator('DtDdWrapper');
			}

		}
	}

}